package com.nantian.nfcm.bms.loan.service;

import com.nantian.nfcm.bms.loan.dao.MsgInfoDao;
import com.nantian.nfcm.bms.loan.entity.MsgInfo;
import com.nantian.nfcm.util.ServiceException;
import com.nantian.nfcm.util.vo.GridData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class MsgInfoService {
    private MsgInfoDao msgInfoDao;

    @Autowired
    public MsgInfoService(MsgInfoDao msgInfoDao) {
        this.msgInfoDao = msgInfoDao;
    }

    /**
     * 查询所有提醒记录
     *
     * @return GridDataServiceException
     */
    public GridData<MsgInfo> findByCondition(String userName){
        if (userName == null || userName.equals("")) {
            return null;
        }
        List<MsgInfo> msgInfos = msgInfoDao.findByUserName(userName);
        GridData<MsgInfo> gridData = new GridData<>();
        gridData.setData(msgInfos);
        gridData.setNumber(msgInfos.size());
        gridData.setPage(1);
        gridData.setTotalPage(0);
        return gridData;
        /*List<Long> msgIds = msgInfoDao.findIdByUserName(userName);
        if(msgIds == null || msgIds.size() ==0 ){
            return null;
        }
        int number = msgIds.size();

        if (number>9){
            msgIds = msgIds.subList(0,9);
        }
        List<MsgInfo> msgInfos = msgInfoDao.findByIds(msgIds);

        GridData<MsgInfo> gridData = new GridData<>();
        gridData.setData(msgInfos);
        gridData.setNumber(number);
        gridData.setPage(1);
        gridData.setTotalPage(0);
        return gridData;*/
        /*Pageable pageable = new PageRequest(0, 10, Sort.Direction.ASC,"msgTime");
        Specification<MsgInfo> specification = (root, query, criteriaBuilder) -> {
            Predicate pUserName = criteriaBuilder.equal(root.get("userName").as(String.class), userName);
            query.where(criteriaBuilder.and(pUserName));
            return query.getRestriction();
        };

        try {
            Page<MsgInfo> msgInfoPage = msgInfoDao.findAll(specification, pageable);
            GridData<MsgInfo> gridData = new GridData<>();
            gridData.setData(msgInfoPage.getContent());
            gridData.setNumber(msgInfoPage.getTotalElements());
            gridData.setPage(msgInfoPage.getNumber());
            gridData.setTotalPage(msgInfoPage.getTotalPages());
            return gridData;
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }*/
    }

    /**
     * 删除提醒信息
     * @param loanId loanId
     * @throws ServiceException ServiceException
     */
    @Transactional
    public void delMsgInfo(String loanId) throws ServiceException{
        List<MsgInfo> msgInfos = msgInfoDao.findByLoanId(loanId);
        if(msgInfos!=null&&msgInfos.size()!=0){
            msgInfoDao.delete(msgInfos);
        }
        else{
            throw  new ServiceException("没有提醒信息");
        }
    }

    /**
     * 根据用户名称删除提醒信息
     * @param userName userName
     * @throws ServiceException ServiceException
     */
    @Transactional
    public void delMsgInfoByUser(String userName) throws ServiceException{
        List<MsgInfo> msgInfos = msgInfoDao.findByUserName(userName);
        if(msgInfos!=null&&msgInfos.size()!=0){
            msgInfoDao.delete(msgInfos);
        }
        else{
            throw  new ServiceException("没有提醒信息");
        }
    }
}